var t = require("../../@babel/runtime/helpers/interopRequireDefault")(require("../../@babel/runtime/regenerator")), e = require("../../@babel/runtime/helpers/asyncToGenerator"), a = getApp();

Page({
    data: {
        topicList: [],
        promptList: [],
        promptCount: 0,
        page: 1,
        pagesize: 10,
        scrollTop: 0,
        shareTitle: "",
        shareImage: "",
        timeline_share_id: 0,
        topic_id: "all"
    },
    onLoad: function(t) {
        var e = this;
        t.sid && wx.setStorageSync("sid", t.sid), this.setData({
            system: a.globalData.system
        }), a.util.login().then(function() {
            e.getTopicList(), e.getPrompts(), e.getWxappInfo();
        });
    },
    switchTopic: function(t) {
        var e = t.currentTarget.dataset.topic;
        this.setData({
            topic_id: e
        }), "vote" == e ? (this.setData({
            page: 1,
            promptList: []
        }), this.getVotePrompts()) : (this.setData({
            page: 1
        }), this.getPrompts());
    },
    getTopicList: function() {
        var t = this;
        a.util.request({
            url: "/write/getTopicList"
        }).then(function(e) {
            t.setData({
                topicList: e.data
            });
        });
    },
    getPrompts: function() {
        var t = this, e = this.data.topic_id, i = this.data.page, r = this.data.pagesize;
        a.util.request({
            url: "/write/getPrompts",
            data: {
                topic_id: e,
                page: i,
                pagesize: r
            }
        }).then(function(e) {
            t.setData({
                promptList: e.data.list,
                promptCount: e.data.count
            }), t.scrollToTop();
        });
    },
    getVotePrompts: function() {
        var t = this, e = this.data.page, i = this.data.pagesize;
        a.util.request({
            url: "/write/getVotePrompts",
            data: {
                page: e,
                pagesize: i
            }
        }).then(function(e) {
            t.setData({
                promptList: e.data.list,
                promptCount: e.data.count
            }), t.scrollToTop();
        });
    },
    doVote: function(t) {
        var e = this, i = t.currentTarget.dataset.id;
        a.util.request({
            url: "/write/votePrompt",
            data: {
                prompt_id: i
            }
        }).then(function(t) {
            a.util.message(t.message);
            var r = e.data.promptList;
            for (var s in r) r[s].id == i && (r[s].isVote = r[s].isVote ? 0 : 1);
            e.setData({
                promptList: r
            });
        });
    },
    scrollToTop: function() {
        var t = this;
        setTimeout(function() {
            t.setData({
                scrollTop: 0
            });
        }, 100);
    },
    toPay: function() {
        wx.navigateTo({
            url: "/pages/pay/pay"
        });
    },
    toWriteChat: function(t) {
        var e = t.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/pages/write/chat?prompt_id=" + e
        });
    },
    getWxappInfo: function() {
        var t = this;
        a.util.request({
            url: "/wxapp/getWxappInfo",
            loading: !1
        }).then(function(e) {
            t.setData({
                shareTitle: e.data.share_title,
                shareImage: e.data.share_image
            });
        });
    },
    onShareAppMessage: function() {
        var a = this;
        return e(t.default.mark(function e() {
            var i;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.next = 2, a.getShareId("wechat");

                  case 2:
                    return i = t.sent, console.log("sh", i), t.abrupt("return", {
                        title: a.data.shareTitle,
                        imageUrl: a.data.shareImage,
                        path: "/pages/write/index?sid=" + i
                    });

                  case 5:
                  case "end":
                    return t.stop();
                }
            }, e);
        }))();
    },
    onShareTimeline: function() {
        var t = this.data.timeline_share_id;
        return t || this.getShareId("timeline"), console.log("share_id", t), {
            title: this.data.shareTitle,
            imageUrl: this.data.shareImage,
            query: "sid=" + t
        };
    },
    getShareId: function(i) {
        var r = this;
        return e(t.default.mark(function e() {
            var s;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return s = 0, t.next = 3, a.util.request({
                        url: "/wxapp/doShare",
                        data: {
                            way: i
                        }
                    }).then(function(t) {
                        s = t.data.share_id, "timeline" == i && r.setData({
                            timeline_share_id: s
                        });
                    });

                  case 3:
                    return t.abrupt("return", s);

                  case 4:
                  case "end":
                    return t.stop();
                }
            }, e);
        }))();
    },
    onAddToFavorites: function() {
        return {
            title: this.data.shareTitle,
            imageUrl: this.data.shareImage
        };
    }
});